package com.oa.web.action;

import com.oa.bean.Dept;
import com.oa.bean.Emp;
import com.oa.service.EmpService;
import com.oa.utils.FileUtils;
import com.oa.utils.PageBean;
import com.oa.web.action.base.BaseAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class EmpAction extends BaseAction<Emp> {

    @Autowired
    private EmpService empService;

    //分页查询
    public String pageQuery() {
        DetachedCriteria dc = pageBean.getCriteria();
        if (model.getIs() != null) {
            //根据员工是否离职查询
            dc.add(Restrictions.eq("is", model.getIs()));
        }

        //根据姓名查询
        if (StringUtils.isNotBlank(model.getTruename())) {
            dc.add(Restrictions.eq("truename", model.getTruename()));
        }

        PageBean pb = empService.pageQuery(this.pageBean);
        this.writeObj2Json(pb, new String[]{"currentPage", "pageSize", "criteria", "emps", "customers"});

        return NONE;
    }

    //员工查询
    public String empQuery() {

        DetachedCriteria dc = this.pageBean.getCriteria();

        //根据姓名查询
        if (StringUtils.isNotBlank(model.getTruename())) {
            dc.add(Restrictions.eq("truename", model.getTruename()));
        }

        //根据性别查询
        if (StringUtils.isNotBlank(model.getSex())) {
            dc.add(Restrictions.eq("sex", model.getSex()));
        }

        //根据部门查询
        if (model.getDept() != null) {
            Integer id = model.getDept().getId();
            if (id != null) {
                if (id == -1) {
                    dc.add(Restrictions.isNull("dept.id"));
                } else {
                    dc.add(Restrictions.eq("dept.id", id));
                }
            }
        }

        //根据员工是否离职查询
        if (model.getIs() != null) {
            dc.add(Restrictions.eq("is", model.getIs()));
        }

        PageBean pb = empService.pageQuery(this.pageBean);
        this.writeObj2Json(pb, new String[]{"currentPage", "pageSize", "criteria", "emps", "customers"});


        return NONE;
    }

    //得到所有员工的姓名
    public void findAllNames() {

        DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);

        Integer is = model.getIs();
        if (is != null) {
            dc.add(Restrictions.eq("is", is));
        }
        Dept dept = model.getDept();
        if (dept != null) {
            Integer id = dept.getId();
            if (id != null) {
                dc.add(Restrictions.eq("dept.id", id));
            }
        }

        List<Emp> list = empService.findAllNames(dc);

        this.writeList2Json(list, new String[]{"currentPage", "pageSize", "criteria", "dept", "customers"});
    }


    //离职员工
    public String departEmp() {

        empService.departEmp(this.model);

        this.writeText("success");
        return NONE;
    }

    //恢复员工
    public String restoreEmp() {

        boolean success = empService.restoreEmp(model);
        this.writeText(success + "");

        return NONE;
    }

    //
    public String validateLoginname() {

        Integer i = empService.validateLoginname(model.getLoginname());
        this.writeText(i + "");
        return NONE;
    }

    //保存
    public String save() {

        empService.save(this.model);

        this.writeText("success");

        return NONE;
    }

    //检查能否离职
    public String checkCanDepart() {
        String msg = empService.checkCanDepart(model.getId());
        this.writeText(msg);
        return NONE;
    }

    //跳转到修改部门
    public String beforeEditEmp() {

        Emp emp = empService.beforeEditEmp(model.getId());

        session.put("emp", emp);

        return "update";
    }

    //修改部门
    public String editEmp() {

//        System.out.println(model);

        //查询之前存在的图片
        Emp emp = empService.findEmpById(model.getId());

        String img = emp.getImg();
        //更新数据库
        empService.updateEmp(model);

        //检查之前图片能否删除
        boolean b = empService.canDelImag(img);

        if (b) {
            String root = ServletActionContext.getServletContext().getRealPath("");
            String path = root + "\\" + img.replace('/', '\\');
            FileUtils.deleteFile(path);
        }

        return NONE;
    }

}
